import { getLyric } from "api/song";
import { ERR_OK } from "api/config";
import { Base64 } from 'js-base64';
class Song {
  constructor({ id, mid, singer, name, album, duration, image, url }) {
    this.id = id;
    this.mid = mid;
    this.singer = singer;
    this.name = name;
    this.album = album;
    this.duration = duration;
    this.image = image;
    this.url = url;
  }

  getLyric() {
    if (this.lyric) {
      return Promise.resolve(this.lyric);
    }
    return new Promise((resolve, reject) => {
      getLyric(this.mid).then((res) => {
        if (res.retcode === ERR_OK) {
          this.lyric = Base64.decode(res.lyric);
          resolve(this.lyric)
        } else {
          reject(new Error('no lyric'))
        }
      })
    })
  }
}

function filterSinger(singer) {
  let ret = [];
  if (!singer) {
    return ''
  }
  singer.forEach((s) => {
    ret.push(s.name)
  })
  return ret.join('/')
}

export default function createSong(musicData, index) {
  return new Song({
    id: musicData.songid,
    mid: musicData.songmid,
    singer: filterSinger(musicData.singer),
    name: musicData.songname,
    album: musicData.albumname,
    duration: musicData.interval,
    image: `https://y.gtimg.cn/music/photo_new/T002R300x300M000${musicData.albummid}.jpg?max_age=2592000`,
    url: getSongUrl(index)
    // url: `http://dl.stream.qqmusic.qq.com/C400${musicData.songmid}.m4a?vkey=64D7390C4E966F1CCE6F183F351FC33FA1DE05F1A8383D522B72861E9B4AC265A6C4FD2D1D52187BC1FB7E48E135CF87DB46DBD084118F62&guid=6876030725&uin=0&fromtag=66`
  })
}
function getSongUrl(index) {
  let _index = index % 5;
  switch (_index) {
    case 0:
      return 'http://dl.stream.qqmusic.qq.com/C400001Qu4I30eVFYb.m4a?vkey=3022DC2E9B74CA9FA0C01F3299C9C8B3D8200A37EA2B1AB958D93289BA29DA6306761401F95FB590880AA8D7CADC32A1E723B069DCD3D333&guid=7426896228&uin=0&fromtag=66'
    case 1:
      return 'http://dl.stream.qqmusic.qq.com/C4000047VwdV487b7t.m4a?vkey=ED24D87DAA360AE4277390B37E505D52DBA18D23A19F2802FDF640BC719EB5E7FCB4D1C3F4942ABAE3977C854A4459429310918CF5F8ADD1&guid=7426896228&uin=0&fromtag=66'
    case 2:
      return 'http://dl.stream.qqmusic.qq.com/C400003v4UL61IYlTY.m4a?vkey=6FA2135F57070C53792675C611F77149CC3FC9BDB021AC7CDBDD93608BE1D8D775823D29A291805454782C8107D8FDCB345C58BB524231CD&guid=7426896228&uin=0&fromtag=66'
    case 3:
      return 'http://dl.stream.qqmusic.qq.com/C400001TXSYu1Gwuwv.m4a?vkey=B7181EDFEDFEF0DDBE579E15137F543F59A239820E7A681E178D22803F4BFEEFF6638F8ABF8996868DDFE5164FBF5493EEA851C5D27DD7AD&guid=7426896228&uin=0&fromtag=66'
    default:
      return 'http://dl.stream.qqmusic.qq.com/C400000QwTVo0YHdcP.m4a?vkey=415C5B8A762F2A43D8DD9EAC046534DF9CA7FF20FCAB4F87FF707468AD3B22743FFAE51E1213FAD80165A10627DDD05624F708FA92E418B4&guid=7426896228&uin=0&fromtag=66'
  }
}